var InvoiceTable = {
    id: "invoiceTable",//表格id
    seItem: null,		//选中的条目
    me : ""
};


/**
 * 初始化整个表格
 */
InvoiceTable.init = function(){
    var defaultColunms = this.initColumn();
    var bstable = new BSTable(InvoiceTable.id,Feng.ctxPath+"/invoice/allParam",defaultColunms);
    bstable.setData({});
    this.me = bstable.init();
};

/**
 * 初始化表格的列
 */
InvoiceTable.initColumn = function(){
    var columns = [
        {title: '序号',align: 'center',valign: 'middle',
            formatter: function indexFormatter(value, row, index) {//在表格前加上序号
                return index+1;
            }},
        {title: '发票类型',field: 'invoiceType',align: 'center',valign: 'middle',
            formatter:function(value,row,index){
                if(value == "1"){
                    return "个人";
                }else if(value == "2"){
                    return "公司";
                }
            }},
        {title: '名称',field: 'invoiceTitle',align: 'center',valign: 'middle'},
        {title: '税号',field: 'taxCode',align: 'center',valign: 'middle'},
        {title: '订单编号',field: 'orderNum',align: 'center',valign: 'middle'},
        {title: '订单金额',field: 'orderPrice',align: 'center',valign: 'middle',
            formatter:function (value,row,index) {
                return numeral(value).format('0,0.00');
            }},
        {title: '申请时间',field: 'applyDate',align: 'center',valign: 'middle'},
        {title: '申请状态',field: 'applyStatus',align: 'center',valign: 'middle',
            formatter:function(value,row,index){
                if(value == "1"){
                    return "申请中";
                }else if(value == "2"){
                    return "已办理";
                }else if(value == "3"){
                    return "已作废";
                }
            }},
        {title: '开票公司',field: 'invoicedCompamy',align: 'center',valign: 'middle'},
        {title: '发票号码',field: 'taxNumber',align: 'center',valign: 'middle'},
        {title: '操作',field: '#',align: 'center',valign: 'middle',
            formatter:function(value,row,index){
                var e = '<button class="btn btn-primary btn-xs" onclick="editBtn(\''+ row.id + '\')" title="编辑"><i class="glyphicon glyphicon-pencil"></i></button> &nbsp;&nbsp;';
                var f = '<button class="btn btn-success btn-xs" onclick="submitBtn(\''+ index + '\')" title="申请确认"><i class="glyphicon glyphicon-ok"></i></button> &nbsp;&nbsp;';
                var g = '<button class="btn btn-danger btn-xs" onclick="cancelBtn(\''+ row.id + '\')" title="申请作废"><i class="glyphicon glyphicon-remove"></i></button> &nbsp;&nbsp;';
                if (row.applyStatus === "2" || row.applyStatus === "3") {
                    return e
                } else {
                    return e + f + g;
                }
            }
        }];
    return columns;
};

/**
 * 点击编辑按钮时，跳出发票申请详情对话框
 * @returns
 */
function editBtn(uid){
    var index = layer.open({
        type: 1,
        title: '发票申请详情',
        skin: 'layui-layer-lan',
        area: [InvoiceInfoDlg.width,InvoiceInfoDlg.height],
        content: InvoiceInfoDlg.cache,
        success: function(layero, index){
            //层弹出成功后回调方法
            InvoiceInfoDlg.renderAdd();
            InvoiceInfoDlg.load(uid);
        },
        maxmin:true,
        scrollbar: false,
        btn:['保存','取消'],
        btnAlign: 'r',  //默认值
        yes:function (index,layero) {
            InvoiceInfoDlg.editSubmit();
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        },btn2:function (index) {
            layer.close(index);
        },
        cancel:function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    InvoiceInfoDlg.currentLayerIndex = index;
}

/**
 * 点击发票申请确认按钮时，跳出发票申请确认对话框
 * @returns
 */
function submitBtn(index){
    var rowData = $("#" + InvoiceTable.id).bootstrapTable('getData')[index];
    var index = layer.open({
        type: 1,
        title: '发票申请确认',
        skin: 'layui-layer-lan',
        area: [InvoiceSubmitDlg.width, InvoiceSubmitDlg.height],
        content: InvoiceSubmitDlg.cache,
        success: function (layero, index) {
            //层弹出成功后回调方法
            InvoiceSubmitDlg.renderAdd();
            InvoiceSubmitDlg.invoiceInfo(rowData)
        },
        maxmin: true,
        scrollbar: false,
        btn: ['确认', '关闭'],
        btnAlign: 'r',  //默认值
        yes: function (index, layero) {
            InvoiceSubmitDlg.invoiceSubmit(rowData.id);
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        }, btn2: function (index) {
            layer.close(index);
        },
        cancel: function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    InvoiceSubmitDlg.currentLayerIndex = index;
}

/**
 * 发票申请作废
 * @returns
 */
function cancelBtn(uid){
    layer.confirm('您确定取消该发票申请?', {
        btn: ['确定', '关闭'] //按钮
    }, function (index) {
        layer.close(index);
        InvoiceSubmitDlg.cancelInvoice(uid);
        InvoiceTable.me.refresh();
    });
}

/**
 * 清空搜索栏
 */
function resetSearchVal() {
    //订单编号
    $("#txt_search_orderNum").val("");
    //名称
    $("#txt_search_invoiceTitle").val("");
    //发票类型
    $("#txt_search_invoiceType").val("-1");
    //申请状态
    $("#txt_search_applyStatus").val("-1");
    //申请时间
    $("#txt_search_applyDate").val("");
}

/**
 * 点击搜索按钮
 * @returns
 */
function clickSearch(){
    var queryData = {};
    //订单号
    var val_search_orderNum = $("#txt_search_orderNum").val();
    if (val_search_orderNum !== "" && val_search_orderNum !== null) {
        queryData['orderNum'] =  val_search_orderNum;
    }
    //发票抬头
    var val_search_invoiceTitle = $("#txt_search_invoiceTitle").val();
    if (val_search_invoiceTitle !== "" && val_search_invoiceTitle !== null) {
        queryData['invoiceTitle'] = val_search_invoiceTitle;
    }

    //发票类型
    var val_search_invoiceType = $("#txt_search_invoiceType").find("option:selected").val();
    if (val_search_invoiceType !== "" && val_search_invoiceType !== null && val_search_invoiceType !== "-1") {
        queryData['invoiceType'] = val_search_invoiceType;
    }

    //申请状态
    var val_search_applyStatus = $("#txt_search_applyStatus").find("option:selected").val();
    if (val_search_applyStatus !== "" && val_search_applyStatus !== null && val_search_applyStatus !== "-1") {
        queryData['applyStatus'] = val_search_applyStatus;
    }

    //申请时间
    var val_search_applyDate = $("#txt_search_applyDate").val();
    if (val_search_applyDate !== "" && val_search_applyDate !== null) {
        queryData['applyDate'] = val_search_applyDate;
    }
    InvoiceTable.me.destroy(queryData);
    // InvoiceTable.me.refresh({query: queryData});
}

/**
 * 导出发票信息
 */
$("#btn_invoice_export").on("click",function () {
    //订单号
    var val_search_orderNum = $("#txt_search_orderNum").val();

    //发票抬头
    var val_search_invoiceTitle = $("#txt_search_invoiceTitle").val();


    //发票类型
    var val_search_invoiceType = $("#txt_search_invoiceType").find("option:selected").val();
    if (val_search_invoiceType == "-1") {
        val_search_invoiceType = "";
    }

    //申请状态
    var val_search_applyStatus = $("#txt_search_applyStatus").find("option:selected").val();
    if (val_search_applyStatus == "-1") {
        val_search_applyStatus = "";
    }

    //申请时间
    var val_search_applyDate = $("#txt_search_applyDate").val();

    var url = Feng.ctxPath + "/invoice/exportInvoice?orderNum="+val_search_orderNum+"&invoiceTitle="
     +val_search_invoiceTitle+"&invoiceType="+val_search_invoiceType+"&applyStatus="+val_search_applyStatus
     +"&applyDate="+val_search_applyDate

    window.location = url;
});

$(function(){
    //初始化发票管理表格
    InvoiceTable.init();
    //初始化发票详情
    InvoiceInfoDlg.init();
    //初始化发票确认
    InvoiceSubmitDlg.init();

});
